package com.zzxz.practicaltrainingsys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzxz.practicaltrainingsys.entity.Homeworkpublish;
import com.zzxz.practicaltrainingsys.mapper.HomeworkpublishMapper;
import com.zzxz.practicaltrainingsys.service.HomeworkpublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class HomeworkpublishServiceImpl implements HomeworkpublishService {
    @Autowired
    private HomeworkpublishMapper homeworkpublishMapper;

    @Override
    public List<Homeworkpublish> getHomeworkpublishById(Integer id) {
        return homeworkpublishMapper.getHomeworkpublishById(id);
    }

    @Override
    public Homeworkpublish getDetailHomeworkpublishById(Integer id) {
        return homeworkpublishMapper.getDetailHomeworkpublishById(id);
    }

    @Override
    public int insertHomeworkpublish(Homeworkpublish homeworkpublish) {
        int i = homeworkpublishMapper.insertHomeworkpublish(homeworkpublish);
        return i;
    }

    @Override
    public int deleteHomeworkpublishById(Integer id) {
        int i = homeworkpublishMapper.deleteHomeworkpublishById(id);
        return i;
    }

    @Override
    public List<String> getHomeworkpublishTypes() {
        return homeworkpublishMapper.getHomeworkpublishTypes();
    }

    @Override
    public List<String> getHomeworkpublishCategory() {
        return homeworkpublishMapper.getHomeworkpublishCategory();
    }

    @Override
    public List<String> getHomeworkpublishName() {
        return homeworkpublishMapper.getHomeworkpublishName();
    }

    @Override
    public List<Map<String, Object>> getAllHomeworkpublish() {
        return homeworkpublishMapper.getAllHomeworkpublish();
    }

    @Override
    public PageInfo<Map<String, Object>> findHomeworkpublishPages(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> lists = homeworkpublishMapper.getAllHomeworkpublish();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

    @Override
    public List<Homeworkpublish> getHomeworkpublishInfo() {
        return homeworkpublishMapper.getHomeworkpublishInfo();
    }

    @Override
    public List<Map<String, Object>> getHomeworkList() {
        List<Homeworkpublish> homeworkList = homeworkpublishMapper.findAll();
        List<Map<String, Object>> result = new ArrayList<>();

        for (Homeworkpublish homework : homeworkList) {
            Map<String, Object> item = new HashMap<>();
            item.put("homework_id", homework.getHomework_id());
            item.put("homework_name", homework.getHomework_name());
            item.put("homework_type", homework.getHomework_type());
            item.put("homework_category", homework.getHomework_category());
            item.put("homework_start", homework.getHomework_start());
            item.put("homework_end", homework.getHomework_end());
            item.put("project_id", homework.getProject_id());

            // 计算状态
            long now = System.currentTimeMillis();
            long start = homework.getHomework_start().getTime();
            long end = homework.getHomework_end().getTime();

            if (now < start) {
                item.put("status", "未开始");
            } else if (now > end) {
                item.put("status", "已结束");
            } else {
                item.put("status", "进行中");
            }

            result.add(item);
        }

        return result;
    }

    @Override
    public List<Map<String, Object>> getHomeworkDetail(Integer homeworkId) {
        System.out.println("Service: 开始获取作业详情，homeworkId: " + homeworkId);

        Homeworkpublish homework = homeworkpublishMapper.findById(homeworkId);
        if (homework == null) {
            System.out.println("Service: 未找到作业信息，homeworkId: " + homeworkId);
            return new ArrayList<>();
        }

        System.out.println("Service: 找到作业信息: " + homework.getHomework_name() +
                ", 类型: " + homework.getHomework_type() +
                ", 项目ID: " + homework.getProject_id());

        // 修复：使用 .equals() 而不是 ==
        if ("小组作业".equals(homework.getHomework_type())) {
            System.out.println("Service: 这是小组作业，查询小组完成情况");
            // 小组作业
            List<Map<String, Object>> result = homeworkpublishMapper.findGroupCompletion(homeworkId, homework.getProject_id());
            System.out.println("Service: 小组作业查询结果数量: " + (result != null ? result.size() : 0));
            if (result != null && !result.isEmpty()) {
                for (Map<String, Object> item : result) {
                    System.out.println("Service: 小组数据: " + item);
                }
            }
            return result;
        } else {
            System.out.println("Service: 这是个人作业，查询个人完成情况");
            // 个人作业
            Map<String, Object> projectInfo = homeworkpublishMapper.findProjectInfo(homework.getProject_id());
            if (projectInfo == null) {
                System.out.println("Service: 未找到项目信息，project_id: " + homework.getProject_id());
                return new ArrayList<>();
            }

            System.out.println("Service: 项目信息: " + projectInfo);

            List<Map<String, Object>> result = homeworkpublishMapper.findIndividualCompletion(
                    homeworkId,
                    (Integer) projectInfo.get("school_id"),
                    (Integer) projectInfo.get("college_id"),
                    (Integer) projectInfo.get("major_id"),
                    (Integer) projectInfo.get("grade_id")
            );

            System.out.println("Service: 个人作业查询结果数量: " + (result != null ? result.size() : 0));
            return result;
        }
    }

    @Override
    public List<Map<String, Object>> getTeamMembers(Integer teamId, Integer homeworkId) {
        System.out.println("Service: 获取小组成员，teamId: " + teamId + ", homeworkId: " + homeworkId);
        List<Map<String, Object>> result = homeworkpublishMapper.findTeamMembers(teamId, homeworkId);
        System.out.println("Service: 小组成员查询结果数量: " + (result != null ? result.size() : 0));
        return result;
    }

    @Override
    public PageInfo<Map<String, Object>> getStudentHomeworks(Integer studentId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> list = homeworkpublishMapper.getStudentHomeworks(studentId);
        return new PageInfo<>(list);
    }

    @Override
    public List<Map<String, Object>> getAllStudentHomeworks(Integer studentId) {
        return homeworkpublishMapper.getStudentHomeworks(studentId);
    }

    @Override
    public int updateHomeworkpublish(Homeworkpublish homeworkpublish) {
        return homeworkpublishMapper.updateHomeworkpublish(homeworkpublish);
    }

    @Override
    public List<Map<String, Object>> getGroupCompletion(Integer homeworkId, Integer projectId) {
        return homeworkpublishMapper.findGroupCompletion(homeworkId, projectId);
    }

    @Override
    public List<Map<String, Object>> getIndividualCompletion(Integer homeworkId, Integer projectId) {
        // 先获取项目信息
        Map<String, Object> projectInfo = homeworkpublishMapper.findProjectInfo(projectId);
        if (projectInfo == null) {
            return Collections.emptyList();
        }

        return homeworkpublishMapper.findIndividualCompletion(
                homeworkId,
                (Integer) projectInfo.get("school_id"),
                (Integer) projectInfo.get("college_id"),
                (Integer) projectInfo.get("major_id"),
                (Integer) projectInfo.get("grade_id")
        );
    }

    @Override
    public List<Map<String, Object>> getHomeworkByTeacherAndProject(Integer teacherId, Integer projectId) {
        try {
            System.out.println("Service层：查询作业列表，教师ID: " + teacherId + ", 项目ID: " + projectId);

            // 参数校验
            if (teacherId == null || projectId == null) {
                System.out.println("参数校验失败：教师ID或项目ID为空");
                return new ArrayList<>();
            }

            List<Map<String, Object>> result = homeworkpublishMapper.getHomeworkByTeacherAndProject(teacherId, projectId);
            System.out.println("Service层：查询成功，返回" + (result != null ? result.size() : 0) + "条数据");

            return result != null ? result : new ArrayList<>();
        } catch (Exception e) {
            System.err.println("Service层：查询作业列表发生异常");
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
}